
local raw = ...

local getmetatable, setmetatable = raw.getmetatable, raw.setmetatable
local pcall = pcall
local getfenv,setfenv = raw.getfenv, raw.setfenv
local error = error

local function constant(value)
	return function()  return value;  end
end

local window_meta do

	local window_getters = {
		title = raw.get_title;
		minimize = constant(raw.minimize);
		minimise = constant(raw.minimize);
	}
	
	local window_setters = {
		title = raw.set_title;
	}

	window_meta = {
		__index = function(self, key)
			local getter = window_getters[key]
			if getter then
				return getter(self)
			end
			error('unrecognised field of window struct',2)
		end;
		__newindex = function(self, key, value)
			local setter = window_setters[key]
			if setter then
				return setter(self, value)
			end
			if window_getters[key] then
				error('attempt to set read-only field "' .. key .. '"', 2)
			end
			error('unrecognised field of window struct',2)
		end;
	}

end

local closebutton_meta do

	local closebutton_getters = {
		down = raw.get_quitrequested;
		struck = raw.get_quitrequested;
		released = raw.get_quitrequested;
	}
	
	closebutton_meta = {
		__index = function(self, key)
			local getter = closebutton_getters[key]
			if getter then
				return getter(self)
			end
			error('unrecognised field of window struct',2)
		end;
		__newindex = function(self, key, value)
			error('cannot modify button struct',2)
		end;
	}

end

return setmetatable({
	closebutton = setmetatable({}, closebutton_meta);
}, window_meta)
